package com.umpay.privi.dao.on.sys.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.umpay.common.persist.AbstractDao;
import com.umpay.common.persist.ibatis.DataAccessException;
import com.umpay.common.persist.ibatis.exception.SQLErrorCodeSQLExceptionTranslator;
import com.umpay.privi.common.RetCode;
import com.umpay.privi.common.XmlData;
import com.umpay.privi.dao.EntityDaoImpl;
import com.umpay.privi.dao.on.sys.RoleDao;
import com.umpay.privi.exception.BusinessException;
import com.umpay.privi.model.sys.Role;

/** 
 * desc:角色Dao实现类
 * <p>创建人：Ding Zhe 创建日期：Apr 19, 2013 </p>
 * @version V1.0  
 */
@SuppressWarnings("unchecked")
public class RoleDaoImpl extends EntityDaoImpl<Role> implements RoleDao {
	private static Logger logger = LoggerFactory.getLogger(RoleDaoImpl.class);
	
	public Role get(Integer roleId )throws DataAccessException{
		try {
			return (Role)getSqlMapClientTemplate().getSqlMapClient().queryForObject("Role.Get", roleId);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public int updateState(Integer roleId, Short state, Short sysId)
			throws DataAccessException {
		Map<String, Object> pamaMap = new HashMap<String, Object>();
		pamaMap.put(XmlData.ROLE_ID, roleId);
		pamaMap.put(XmlData.STATE, state);
		pamaMap.put(XmlData.SYSID, sysId);
		logger.debug(String.format("[角色Dao]更新角色状态，参数%s", pamaMap));
		try {
			return getSqlMapClientTemplate().getSqlMapClient().update("Role.UpdateByMap", pamaMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public void delete(Map<String, Object> paraMap) throws DataAccessException,
			BusinessException {
		if (!paraMap.containsKey(XmlData.ROLE_ID)) {
			throw new BusinessException(RetCode.REQ_PARA_MISSING,"角色号为必传字段");
		}
		logger.debug(String.format("[角色Dao]标记删除角色，参数%s", paraMap));
		try {
			//更新Flag标记为1
			int count = getSqlMapClientTemplate().getSqlMapClient().update("Role.FlagDelete", paraMap);
			//判断更新记录数
			if (1 != count) {
				//更新记录数不为1，表示角色不存在
				throw new BusinessException(RetCode.ROLE_ISNOT_EXIT,"角色信息不存在");
			}
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public void update(Map<String, Object> paraMap) throws DataAccessException,BusinessException {
		logger.debug(String.format("[角色Dao]更新角色信息，参数%s", paraMap));
		try {
			//更新Flag标记为1
			int count = getSqlMapClientTemplate().getSqlMapClient().update("Role.UpdateByMap", paraMap);
			//判断更新记录数
			if (1 != count) {
				//更新记录数不为1，表示角色不存在
				throw new BusinessException(RetCode.ROLE_ISNOT_EXIT,"角色信息不存在");
			}
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public List<Role> findBy(Map<String, Object> paraMap)
			throws DataAccessException {
		logger.debug(String.format("[角色Dao]查询角色列表，参数%s", paraMap));
		try {
			return getSqlMapClient().queryForList("Role.FindWithMap", paraMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public List<Role> findRoleList(Map<String, Object> reqMap) throws DataAccessException {
		try {
			return getSqlMapClient().queryForList("Role.findRoleListByKeys",reqMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public List<Role> findRolePageListByKeys(Map<String, Object> reqMap,Integer startIndex,Integer pageSize) throws DataAccessException {
		try {
			return getSqlMapClient().queryForList("Role.findRoleListByKeys",reqMap,startIndex,pageSize);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public Integer findRolePageListByKeys_count(Map<String, Object> reqMap) throws DataAccessException {
		try {
			return (Integer)getSqlMapClient().queryForObject("Role.findRoleListByKeys_count", reqMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public List<Map<String, Object>> findRolesByCreator(Map<String, Object> reqMap)
			throws DataAccessException {
		try {
			return (List<Map<String, Object>>)getSqlMapClient().queryForList("Role.queryRolesByCreator", reqMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
}



